package com.example.ex2.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ex2.domain.Info;
import com.example.ex2.domain.Item;
import com.example.ex2.domain.Returninfo;
import com.example.ex2.persistence.BorrowinfoMapper;
import com.example.ex2.persistence.ItemMapper;
import com.example.ex2.service.BorrowInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BorrowInfoServiceImpl implements BorrowInfoService {

    @Autowired
    private BorrowinfoMapper borrowinfoMapper;
    @Autowired
    private ItemMapper itemMapper;

    @Override
    public boolean addBorrowInfo(Info info) {
        Item item = new Item();
//        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        item = itemMapper.selectById(info.getItem_id());
        System.out.println(item);
        item.setRemain_num(item.getRemain_num()-1);
        itemMapper.updateById(item);
        return (borrowinfoMapper.insert(info)>0);
    }

    @Override
    public boolean deleteBorrowInfoByID(int id) {
        return (borrowinfoMapper.deleteById(id)>0);
    }

    @Override
    public List<Info> selectAllBorrowInfo() {
        QueryWrapper<Info> queryWrapper = new QueryWrapper<>();
        return borrowinfoMapper.selectList(queryWrapper);
    }

    @Override
    public boolean updateItemByBorrowInfo(Info info) {
        return (borrowinfoMapper.updateById(info)>0);
    }

    @Override
    public List<Info> selectNoReturnedBorrowInfoByUserId(int user_id){
        QueryWrapper<Info> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("return_state",0).eq("user_id",user_id);
        return (borrowinfoMapper.selectList(queryWrapper));
    }

}
